
package net.worlds.object;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.worlds.Main;
import net.worlds.WorldsException;
import net.worlds.component.AddTextureDialog;
import net.worlds.lua.Event;
import net.worlds.ui.UI;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.LuaCallFrame;
import se.krka.kahlua.vm.LuaClosure;

/**
 *
 * @author Jacob
 */
public class Editor extends GameObject{
    AddTextureDialog dialog;
    public Editor() throws WorldsException
    {
        super("Editor");
        this.addEvent("SelectionChanged", new Event(), "");
        this.addEvent("MouseModeChanged", new Event(), "");
        this.addFunction("world", new JavaFunction()
        {

            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                callFrame.push(Main.getGame().getCurrentWorld().getRootObject());
                return 1;
            }
            
        });
        this.addFunction("addActionButton", new JavaFunction()
        {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                LuaClosure c = (LuaClosure) callFrame.get(1);
                String i = callFrame.get(2).toString();
                String tt = callFrame.get(3).toString();
                Main.getGame().addActionButton(c, i, tt);
                return 0;
            }
        });
        this.addFunction("clearActionButtons", new JavaFunction()
        {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                Main.getGame().clearActionButtons();
                return 0;
            }
        });
        this.addFunction("showInsertTextureDialog", new JavaFunction()
        {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                GameObject obj = (GameObject) callFrame.get(1);
                if(dialog != null)
                    dialog.dispose();
                dialog = new AddTextureDialog(obj);
                dialog.addComponentListener(new ComponentListener()
                {
                    @Override
                    public void componentResized(ComponentEvent e) {

                    }

                    @Override
                    public void componentMoved(ComponentEvent e) {
                    }

                    @Override
                    public void componentShown(ComponentEvent e) {
                    }

                    @Override
                    public void componentHidden(ComponentEvent e) {
                        dialog = null;
                    }
                });
                dialog.setVisible(true);
                return 0;
            }
        });
        addFunction("showDefaultColorPicker", new JavaFunction()
        {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                UI.showDefaultColorPicker();
                return 0;
            }
        });
    }
}
